require 'rubygems'
require 'chronic'

class DateTimeSelector < Pan::Component
  def initialize(owner)
    @owner = owner
    @val = "today"
  end

  def parse(str)
    Chronic.parse(str) 
  end


  def render_on(html)
    html.div("class"=>"date-time-selector") {
      html.form {
        html.input("value"=>@val) {|val| 
          @val = val
          @owner.date_time_selected(parse(val))
        }
      }
    }
  end
end

class DateSelector < DateTimeSelector
  def parse(str)
    time = super(str)
    if time
      time.to_date
    else
      "could not parse '#{str}'"
    end
  end


  def render_on(html)
    html.div("class"=>"date-selector") {
      html.form {
        html.input("value"=>@val) {|val| 
          @val = val
          @owner.date_selected(parse(val))
        }
      }
      html.img_link("src"=>"/pm_media/img/info_icon.png") {
        push(About.new(self))
      }
    }
  end

  class TemplateComponent < Pan::Component
    def render_on(html)
      render_template(html)
    end
  end

  class About < TemplateComponent
    def initialize(owner)
      @owner = owner
    end
  end
end

